//============================================================================
// Name        : MiniBeh.cpp
// Author      : 
// Version     :
// Copyright   : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================

#include "BCore.h"
using namespace std;

#include <boost/thread.hpp>
using namespace boost;

#include "Signals.hpp"

//set<Logger::LOGTAG> Logger::avaliables_tags;
//const Logger::LOGTAG Logger::LOGTAG_ALL="ALL";
//struct init_avaliables_tags_t{init_avaliables_tags_t(){
//	Logger::avaliables_tags.insert(Logger::LOGTAG_ALL);
//}};
//init_avaliables_tags_t init_avaliables_tags;

struct MySig{int i; MySig(int i=0):i(i){}};
Signal<MySig> signal;

class B1:public Behavior{
public:
	int run(){
		Signal<MySig>::Listener sig(signal);
		for( int i=0;i<10;i++){
			if( isTerminated() ) return terminatedCode();

			Logger(std::cout,"ALL")<<object("b1")<<"B1["<<i<<"]"<<close;
			boost::this_thread::sleep(boost::posix_time::seconds(1));
			signal.push(MySig(i*10));
		}
		return 1;
	}
};



class B2:public Behavior{
public:
	int run(){
		Signal<MySig>::Listener sig(signal);
		for( int i=0;i<5;i++){
			if( isTerminated() ) return terminatedCode();

			MySig ms = sig.wait();

			Logger(std::cout,"ALL")<<object("b2")<<"B2["<<i<<"]"<<": "<<ms.i<<close;
			//boost::this_thread::sleep(boost::posix_time::seconds(1));
		}
		return 2;
	}
	std::string str()const{return "B2";}
};

Logger syslog(){ return Logger(std::cout,"ALL")<<object("system"); }

int main() {

	B1 b1;
	B2 b2;
	Parallel p;

	p.run(b1);
	p.run(b2);
	int res = p.untilFirstExit();
	syslog()<< ((B2&)p.fistStopped()).str() <<close;

	stringstream s; s<<"result is "<<res;
	syslog()<<s.str()<<close;


	syslog()<<std::string("END OF PROGRAM")<<close;
	return 0;
}
